package com.bphx.android.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.bphx.android.IStorage;
import com.bphx.android.activities.dialogs.ErrorDialog;
import com.bphx.android.activities.views.RemovableEditText;
import com.bphx.android.db.Storage;
import com.bphx.android.exception.BaseException;
import com.bphx.android.ui.ITextView;
import com.bphx.android.ui.listeners.IActionListener;
import com.bphx.android.words.Word;

import java.util.ArrayList;
import java.util.List;

import static com.bphx.android.R.id.*;
import static com.bphx.android.R.layout.edit_word_layout;
import static com.bphx.android.exception.ExceptionFactory.getWordCreationException;
import static com.bphx.android.utils.Constants.WORD_KEY;
import static com.bphx.android.utils.Utils.getTextFrom;
import static com.bphx.android.words.WordExceptionType.WORD_HAVE_NOT_VARIANTS;
import static com.bphx.android.words.WordExceptionType.WORD_IS_EMPTY;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class CreateWordActivity extends Activity implements IActionListener<View> {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(edit_word_layout);

        originalWordField = (EditText) findViewById(ETOriginalWord);
        variantsLayout = (ViewGroup) findViewById(layoutVariants);
        invalidVariantsLayout = (ViewGroup) findViewById(layoutInvalidVariants);

        variants = new ArrayList<ITextView>();
        invalidVariants = new ArrayList<ITextView>();
        initButtons();
    }

    protected void dispatchOk() {
        IStorage storage = new Storage(this);

        try {

            Word word = createWord();
            storage.addWord(word);
            getIntent().putExtra(WORD_KEY, word);
            setResult(RESULT_OK, getIntent());
            finish();
        } catch (BaseException e) {

            new ErrorDialog(this, e.getMessage()).show();
        }
    }

    private void initButtons() {

        findViewById(cancelButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });

        findViewById(okButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dispatchOk();
            }
        });

        findViewById(btnAddVariant).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RemovableEditText variant = new RemovableEditText(CreateWordActivity.this, CreateWordActivity.this);
                variants.add(variant);
                variantsLayout.addView(variant);
            }
        });

        findViewById(btnAddInvalidVariant).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RemovableEditText variant = new RemovableEditText(CreateWordActivity.this, CreateWordActivity.this);
                invalidVariants.add(variant);
                invalidVariantsLayout.addView(variant);
            }
        });
    }

    protected Word createWord() throws BaseException {

        String originalWord = getTextFrom(originalWordField);

        if (isBlank(originalWord)) {
            throw getWordCreationException(WORD_IS_EMPTY, this);
        }

        Word word = new Word(originalWord);

        for (ITextView v : variants) {
            word.addVariant(v.getText());
        }

        for (ITextView v : invalidVariants) {
            word.addInvalidVariant(v.getText());
        }

        if (word.getVariants().isEmpty() || word.getInvalidTranslations().isEmpty()) {
            throw getWordCreationException(WORD_HAVE_NOT_VARIANTS, this);
        }

        return word;
    }

    @Override
    public void dispatchAction(View sender) {

        ((ViewGroup) sender.getParent()).removeView(sender);

        variants.remove((ITextView) sender);
        invalidVariants.remove((ITextView) sender);
    }

    protected ViewGroup variantsLayout;
    protected ViewGroup invalidVariantsLayout;

    protected EditText originalWordField;
    protected List<ITextView> variants;
    protected List<ITextView> invalidVariants;
}